<?php

namespace App\Http\Controllers\Admin;

use App\Http\Classes\Admin;
use App\Http\Classes\Material;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;

class AdminController extends Controller
{
    //
    public function index()
    {
        try
        {
            if ( Session::has('ADMIN_INFO') )
            {
                return redirect('/admin/home');
            }
            else
            {
                if ( isset($_COOKIE['ADMIN_ID']) && isset($_COOKIE['ADMIN_TOKEN']) )
                {
                    $admin = new Admin( $_COOKIE['ADMIN_ID'] );
                    $admin = $admin->loginByCookie( $_COOKIE['ADMIN_TOKEN'] );
                    if ( $admin )
                    {
                        Session::put('ADMIN_INFO', $admin);
                        return redirect('/admin/home');
                    }
                    else
                    {
                        return view('admins.login');
                    }
                }
                else
                {
                    return view('admins.login');
                }
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionView($exception, __FUNCTION__);
        }
    }

    public function login(Request $request)
    {
        try
        {
            $data = $request->all();
            if ( isset($data['admin']) && isset($data['password']) )
            {
                $admin = new Admin();
                $info = $admin->login($data['admin'], $data['password']);
                if ( $info )
                {
                    Session::put('ADMIN_INFO', $info);
                    setcookie('ADMIN_ID', $info->id, time()+DAY7, '/');
                    setcookie('ADMIN_TOKEN', $info->token, time()+DAY7, '/');
                    return redirect('/admin/home');
                }
                else
                {
                    setcookie('ADMIN_ID', '', time()-3600, '/');
                    setcookie('ADMIN_TOKEN', '', time()-3600, '/');
                    return redirect('/admin/index');
                }
            }
            else
            {
                return redirect('/admin/index');
            }
            //return view('admins.home');
        }
        catch (\Exception $exception)
        {
            return $this->exceptionView($exception, __FUNCTION__);
        }
    }

    public function logout()
    {
        try
        {
            Session::forget('ADMIN_INFO');
            setcookie('ADMIN_ID', '', time()-3600, '/');
            setcookie('ADMIN_TOKEN', '', time()-3600, '/');
            return redirect('/admin/index');
        }
        catch (\Exception $exception)
        {
            return $this->exceptionView($exception, __FUNCTION__);
        }
    }

    public function home()
    {
        try
        {
            $material = new Material();
            $materials = $material->getMaterials();
            if ( $materials )
            {
                return view('admins.home')->with('materials', $materials);
            }
            else
            {

            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionView($exception, __FUNCTION__);
        }
    }
}
